//
//  MindDetailController.m
//  MindBox
//
//  Created by MS on 16-1-13.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "MindDetailController.h"
#import "GoodsDetailController.h"
#import "GoodsController.h"
#import "MindPageView.h"
#import "Discover_MindPageTool.h"
#import "Mind_DetailModel.h"
#import "NSString+Components.h"
@interface MindDetailController ()<MindPageViewDelegate,GiftPageViewDelegate>
@property (nonatomic,strong) NSMutableArray * itemGoodsArray;
@property (nonatomic,weak) MindPageView * mindView;
@property (nonatomic,assign) int pages;
@property (nonatomic,strong) Param * param;
@property (nonatomic,strong) Mind_Banner * mind_banner;
@end

@implementation MindDetailController

-(Param *)param
{
    if (_param==nil) {
        _param = [Param param];
    }
    return _param;
}

-(NSMutableArray *)itemGoodsArray
{
    if (_itemGoodsArray==nil) {
        _itemGoodsArray = [NSMutableArray array];
    }
    return _itemGoodsArray;
}

-(MindPageView *)mindView
{
    if (_mindView==nil) {
        MindPageView * view = [[MindPageView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height-64)];
        _mindView = view;
        
        __weak typeof(self) weakSelf = self;
        [_mindView setMindPageViewBlock:^{
            
            GoodsController * vc  = [[GoodsController alloc] init];
            vc.good_id = weakSelf.mind_banner.table_id;
            vc.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
            
        }];
        
        _mindView.delegate_min = self;
        _mindView.delegate = self;
        [self.view addSubview:view];
        
    }
    return _mindView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadGoodsListDataWithParam:self.param];
    
    [self addFootRefresh];
    [self addHeaderRefresh];
}


-(void)loadGoodsListDataWithParam:(Param *)param
{
    param.search_type = @"7";
    param.activity_id = self.activity_id;

    param.page = ++_pages;

    [Discover_MindPageTool mindPageListDataWithParam:param Success:^(Mind_Detail *data) {
       
        Mind_Banner * mind_banner =[data.banner firstObject];
        self.mind_banner = mind_banner;
        [self loadBannerDataWithGoods_id:[mind_banner table_id]];
        [_mindView.collectionView.mj_footer endRefreshing];
        [_mindView.collectionView.mj_header endRefreshing];
        
        if (data.goods.count) {
            [self.itemGoodsArray addObjectsFromArray:data.goods];
        }
        
        self.mindView.gift_ItemGoods = self.itemGoodsArray;
        self.mindView.mind_DetailData = data;
        
        [self setNavBar];
        
    } failure:^(NSError *error) {
        
    }];
}


-(void)loadBannerDataWithGoods_id:(NSString *)good_id
{
    [Discover_MindPageTool mindBannerDataWithParam:good_id Success:^(GoodsDetailModel *data) {
        
        self.mind_banner.short_descrip = data.short_description;
        self.mindView.mind_Banner = self.mind_banner;
        
    } failure:^(NSError *error) {
        
        
    }];
    
}


-(void)mindPageMenuViewDidClick:(MindPageView *)mindView param:(Param *)param index:(NSInteger)index
{
    self.param = param;
    self.pages = 0;
    [self.itemGoodsArray removeAllObjects];
    [self loadGoodsListDataWithParam:param];
}

-(void)giftCollectionViewDidClick:(GiftPageView *)giftView itemGoods:(Gift_ItemGoods *)goods index:(NSInteger)index
{
    GoodsController * vc  = [[GoodsController alloc] init];
    vc.good_id = goods.goods_id;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)addFootRefresh
{
    __weak typeof(self) weakSelf = self;
    
    self.mindView.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadGoodsListDataWithParam:weakSelf.param];
    }];
    
}

-(void)addHeaderRefresh
{
    __weak typeof(self) weakSelf = self;
    
    self.mindView.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pages = 0;
        [weakSelf.itemGoodsArray removeAllObjects];
        [weakSelf loadGoodsListDataWithParam:weakSelf.param];
    }];
}

-(void)setNavBar
{
    
    MyNavigationItem * leftItem = [MyNavigationItem navigationItemWithImage:@"left_go" andHighlightImage:@"left_go" andTag:0 andTitle:nil addTarget:self action:@selector(leftBtnClick)];
    
    MyNavigationItem * rightItem =[MyNavigationItem navigationItemWithImage:@"threedot" andHighlightImage:@"threedot" andTag:0 andTitle:nil addTarget:self action:@selector(rightClick)];
    
    MyNavigationBar * navBar = [[MyNavigationBar alloc]initWithFrame:CGRectMake(0, 20, self.view.width, 44)];
    navBar.backgroundColor = [UIColor whiteColor];
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, navBar.frame.size.height-0.5, navBar.frame.size.width, 0.5)];
    lineView.backgroundColor =[UIColor grayColor];
    [navBar addSubview:lineView];
   
    [navBar setNavigationBarWithBgImageName:@"无" andTitle:self.title  andTitleView:nil  andLeftItems:@[leftItem] andRightItems:@[rightItem]];
    [self.view addSubview:navBar];
}

-(void)leftBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)rightClick{
    
    
}

-(void)dealloc
{
//    NSLog(@"MindDetailController卸载");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
